/*
 * @Author: zxc
 * @Date: 2020-10-16 14:32:10
 * @LastEditTime: 2020-10-16 14:49:13
 * @LastEditors: zxc
 * @Description: 循环队列
 */


class MyCircularQueue {
  constructor(capacity) {
      this.cap = capacity;
      this.head = -1;
      this.tail = -1;
      this.arr = [];
  }
  enQueue(value) {
      if(this.isFull()){
          return false;
      }
      if (this.isEmpty()) {
          this.head = 0;
      }
      this.tail = (this.tail + 1) % this.cap;
      this.arr[this.tail] = value;
      return true;
  }
  deQueue() {
      if(this.isEmpty()){
          return false;
      }
      if(this.head == this.tail){
          this.head = this.tail = -1;
      }else{
          this.head = (this.head + 1) % this.cap;
      }
      return true;
  }
  Front() {
      if (this.isEmpty()) {
          return -1;
      }
      return this.arr[this.head];
  }
  Rear() {
      if (this.isEmpty()) {
          return -1;
      }
      return this.arr[this.tail];
  }
  isEmpty() {
      return this.head == -1;
  }
  isFull() {
      return this.head == (this.tail + 1) % this.cap;
  }
}

let a = new MyCircularQueue(10)
a.enQueue('test1')
a.enQueue('test2')
a.enQueue('test3')
a.enQueue('test3.1')
a.enQueue('test3.2')
a.enQueue('test3.3')
a.enQueue('test3.4')
a.enQueue('test3.5')
a.enQueue('test3.6')
a.enQueue('test4')
a.deQueue()
a.enQueue('test5')
console.log(a)